//
//  RegistViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/16.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "RegistViewController.h"
#import "Reigst.h"
#import "DoctorIfo.h"
#import "SVProgressHUD.h"
@interface RegistViewController ()<UITextFieldDelegate>
{
    __weak IBOutlet UITextField *_phNumber;
    
    __weak IBOutlet UITextField *_passWard;

    __weak IBOutlet UITextField *_sPassWard;
    
    __weak IBOutlet UITextField *_sAcCode;
   
    NSInteger editTag;
  
    
}
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(0, 0, 26, 40);
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
     backBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    // Do any additional setup after loading the view.
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)active:(id)sender {

          if(![self isPhoneNumberOfString:_phNumber.text])
        {
           [SVProgressHUD showImage:nil status:@"手机号输入不正确，请重新输入"];
            return;
        }
     else if(_passWard.text.length==0)
     {
         [SVProgressHUD showImage:nil status:@"请输入密码"];
     }
     else if(_sPassWard.text.length==0)
     {
         [SVProgressHUD showImage:nil status:@"请再次输入密码"];
     }
    else if(!([_passWard.text isEqualToString:_sPassWard.text]&&_sPassWard.text!=NULL))
        {
             [SVProgressHUD showImage:nil status:@"两次密码输入不相符，请重新输入"];
          
            return;
        }
    else if(_sAcCode.text.length==0)
    {
        [SVProgressHUD showImage:nil status:@"请输入激活码"];
        
        return;

        
    }
        else
        {
            [Reigst registWithUsername:_phNumber.text Password:_passWard.text And:_sAcCode.text success:^(Reigst *registe) {
             
                if(registe.code.integerValue==1)
                {
                    
                    [SVProgressHUD showWithStatus:@"激活成功，正在登陆中..." maskType:SVProgressHUDMaskTypeBlack];
                   
                    [DoctorIfo loginWithUsername:_phNumber.text Password:_passWard.text success:^(DoctorIfo *doctor) {
                        [SVProgressHUD dismiss];
                 CATransition *animation = [CATransition animation];
                [animation setDuration:0.4];
                [animation setType: kCATransitionPush];
                
                [animation setSubtype: kCATransitionFromBottom];
                [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
                
                [self.navigationController.view.layer addAnimation:animation forKey:nil];
                
                [self.navigationController popToRootViewControllerAnimated:NO];
                    } failure:^(NSError *error) {
                        NSLog(@"%@",error.localizedDescription);
                        
                    }];

                }
                else
                {
                    [SVProgressHUD showImage:nil status:registe.msg];
                                   }
               

            } failure:^(NSError *error) {
                [SVProgressHUD showImage:nil status: @"激活失败,请重试"];
               
                NSLog(@"%@",error.localizedDescription);
            }];
        
            
           
            
        }
    

}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    editTag=textField.tag;
}



//空白页点击键盘消失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITextView *text = (UITextView *)[self.view viewWithTag:editTag];
   
    
    [text resignFirstResponder];
    
    
}

-(BOOL)isPhoneNumberOfString:(NSString *)phoneString{
   
    NSMutableString *str3=[[NSMutableString alloc]init];
    int count=0,sum=0;
    if(phoneString.length>=1)
    {
    NSString *str1=[phoneString substringToIndex:1];
    
    if ([str1 isEqualToString:@"1"]==YES)
    {
        
        ++sum;
        
        [str3 appendString:@"第一个号码为:1"];
        
         }
    }
  
    
    for (NSUInteger i=0; i<[phoneString length]; i++) {
        
        unichar c=[phoneString characterAtIndex:i];
        
        if(c>='0'&&c<='9')
        {
            
            ++count;
        }
        
    }
    return (count==11&&sum==1);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
